// 获取token
import {
  addCategorySql,
  addUserSql, categoryListSql,
  findUserInfo,
  findUserOne,
  isUser,
  IUser,
  jurisdictionsType,
  qx,
  updateToken, updateUserSql, userListSql,
} from "@src/sql/userSql";
import {pwdCompare, pwdHash} from "@src/util/pwdUtill";
import {clone, getMeta, getToken} from "@src/util/util";
import moment from "moment";
import CONSTANT from "@src/util/constant";
import Jurisdiction from "@src/models/jurisdiction";

// 获取登录信息
export const getJt = async (username: string, password: string) => {
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  const user = await findUserOne(username) as IUser | null;
  //用户名未查到
  if (!user) {
    resObj = CONSTANT.LOGIN_ERROR;
    return resObj;
  }
  //原密码
  const pwd = user.password ?? "";
  //密码验证
  const isPwd = await pwdCompare(password, pwd);
  //密码错误
  if (!(isPwd)) {
    resObj = CONSTANT.LOGIN_ERROR;
    return resObj;
  }
  // token转换
  const token = getToken({
    id: user.id,
    username: user.username,
    password: pwd,
  });
  // 更新token
  await updateToken(username, token);
  return {
    ...resObj,
    data: {
      token,
    },
  };
};

// 新增用户
export const addUser = async (username: string, psd: string, jurisdiction: string[]) => {
  // 字段是否存在
  const count = await isUser(username);
  // 用戶已存在
  if (count === 1) {
    return false;
  }
  // 加密后的hash
  const password = await pwdHash(psd);
  // 开始时间戳
  const time = moment().format("X");
  // 权限查询
  const jurisdictionData = await Jurisdiction.findAll({
    attributes: ['id', 'name', 'english_name'],
    where: {
      id: jurisdiction,
    },
  });
  // 新建用户
  await addUserSql(username, password, jurisdictionData, time);
  return true;
};

// 个人信息接口
export const userInfo = async (id: string) => {
  const info = await findUserInfo(id) as IUser | null;
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  if (!info) {
    resObj = CONSTANT.SERVER_ERROR;
    return resObj;
  }
  return {
    ...resObj,
    data: {
      id: info.id,
      jurisdictions: info.jurisdictions,
      created_at: info.created_at,
      lasted_at: info.lasted_at,
      username: info.username,
    },
  };
};

// 更新用户
export const updateUser = async (id: string, username: string, jurisdictions_ids: jurisdictionsType[]) => {
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  if (!username) {
    resObj = CONSTANT.USERNAME_PARAMS_ERROR;
    return resObj;
  }
  if (!jurisdictions_ids.length) {
    resObj = CONSTANT.JURISDICTIONS_PARAMS_ERROR;
    return resObj;
  }
  await updateUserSql(id, username, jurisdictions_ids);
  return resObj;
};

// 用户列表
export const userList = async (page: number, size: number, filters: any) => {
  // 从第几条开始
  const offset = (page - 1) * size;
  // 执行sql
  const {count, rows} = await userListSql(offset, size, filters);
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  const meta = getMeta(count, page, size);
  return {
    ...resObj,
    meta,
    data: rows,
  };
};

// 权限生成
export const qxServer = async () => {
  await qx();
};

// 分类生成
export const categoryServer = async () => {
  await addCategorySql();
};
